package com.fingard.dsp.bank.directbank.zyb01;

import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.detail.RetAtsBatPayRecDetail;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class ZYB01BatPayRec extends ZYB01Base implements IBatPayRec {
    //1988
    public RetAtsBatPayRec d_1988_BatPay(ReqBatPayRec p_req1988) throws Exception {
        return d_BatPayRec(p_req1988);
    }

    //9188
    @Override
    public RetAtsBatPayRec d_9188_BatPay(ReqBatPayRec p_req9188) throws Exception {
        return null;
    }

    //批量代收和代发工资提交
    public RetAtsBatPayRec d_BatPayRec(ReqBatPayRec p_reqBat) throws Exception {
        RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_reqBat.reqHeader);
        try {
            ZYB01Header tmpBnkHeader = new ZYB01Header();
            String sigData = "1";
            String charsetName = "UTF-8";
            // 198801 批量代付05B2EBankInnerBatchTransfer 198802 代发工资05B2EAgentPaySalary
            if (p_reqBat.reqHeader.transType.equals("1988")) {
                if (p_reqBat.reqHeader.commandCode.equals("198801")) {
                    tmpBnkHeader.trsCode = "05B2EBankInnerBatchTransfer";//批量代付
                } else if (p_reqBat.reqHeader.commandCode.equals("198802")) {
                    tmpBnkHeader.trsCode = "05B2EAgentPaySalary";//代发工资
                }
            }
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            writeCommonHeader(xmlWriter, tmpBnkHeader, reqHeader.reqSeqID);
            /*<Body>
			<PayerAcNo>521012735403012</PayerAcNo>
			<PayerAcName></PayerAcName>
			<TotalAmount>22</TotalAmount>
			<TotalCount>2</TotalCount>
			<List>
			<Row>
			<PayeeAcNo>0402024409300161221</PayeeAcNo>
			<PayeeAcName>800003208</PayeeAcName>
			<Amount>10.00</Amount>
			<Remark></Remark>
			</Row>
			</List>
			</Body>
			</Message>*/
            xmlWriter.writeStartElement("Body");
            xmlWriter.writeElementString("PayerAcNo", p_reqBat.ownAct);
            xmlWriter.writeElementString("PayerAccountName", p_reqBat.ownActName);
            xmlWriter.writeElementString("TotalAmount", p_reqBat.totalAmt);
            StringBuffer sbMsg = new StringBuffer();
            if (p_reqBat.reqHeader.commandCode.equals("198801")) {
                if (Integer.parseInt(p_reqBat.totalNum) <= 500) {
                    xmlWriter.writeElementString("TotalCount", p_reqBat.totalNum);
                } else {
                    retAts.respCode = "0";
                    retAts.respInfo = "超过批量支付最大支持总笔数";
                    retAts.reqSeqID = p_reqBat.reqBatchNO;
                    retAts.transState = PaymentState.FAIL_Batch;
                    retAts.payInfoCode = ConnState.FAIL;
                    retAts.writeRetToATS();
                    return retAts;
                }
            } else if (p_reqBat.reqHeader.commandCode.equals("198802")) {
                if (Integer.parseInt(p_reqBat.totalNum) <= 1000) {
                    xmlWriter.writeElementString("TotalCount", p_reqBat.totalNum);
                } else {
                    retAts.respCode = "0";
                    retAts.respInfo = "超过批量支付最大支持总笔数";
                    retAts.reqSeqID = p_reqBat.reqBatchNO;
                    retAts.transState = PaymentState.FAIL_Batch;
                    retAts.payInfoCode = ConnState.FAIL;
                    retAts.writeRetToATS();
                    return retAts;
                }
            }
            xmlWriter.writeStartElement("List");
            while (p_reqBat.readRecord()) {
                xmlWriter.writeStartElement("Row");
                xmlWriter.writeElementString("PayeeAcNo", p_reqBat.readedDetail.recAct);
                xmlWriter.writeElementString("PayeeAcName", p_reqBat.readedDetail.recName);
                xmlWriter.writeElementString("TrsAmount", p_reqBat.readedDetail.payAmount);
                xmlWriter.writeElementString("Remark", p_reqBat.readedDetail.abstractATS);//传入对账码
                String payeeType = "I";
                if (p_reqBat.reqHeader.commandCode.equals("198801")) {
                    if (p_reqBat.readedDetail.sameBnk.equals("1")) {
                        if (p_reqBat.readedDetail.isPrivate.equals("1")) {
                            payeeType = "P";
                        } else {
                            payeeType = "I";
                        }
                        xmlWriter.writeElementString("PayeeType", payeeType);
                    }
                }
                xmlWriter.writeEndElement();
            }
            xmlWriter.writeEndElement();
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();//写入报文完成
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), tmpBnkHeader, sigData, charsetName);//发送银行
			/*tmpStrRet[0] = "";
			tmpStrRet[1] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
					"<Reply_Msg>\n" +
					" <Sys_Head>\n" +
					"    <ReturnCode>000000</ReturnCode>\n" +
					"    <ReturnMsg></ReturnMsg>\n" +
					" </Sys_Head>\n" +
					" <Body>\n" +
					"    <IBSJnlNo>1849938</IBSJnlNo>\n" +
					" </Body>\n" +
					"</Reply_Msg> ";*/
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpStatus = tmpBkReader.getFirstText("/Reply_Msg/Sys_Head/ReturnCode");
                    String tmpInfo = tmpBkReader.getFirstText("/Reply_Msg/Sys_Head/ReturnMsg");
                    if (tmpStatus != null && tmpStatus.length() > 0) {
                        retAts.respCode = "0";
                        retAts.reqSeqID = p_reqBat.reqBatchNO;
                        if (isActiveRetCode(tmpStatus)) {
                            retAts.respInfo = tmpInfo;
                            retAts.transState = tmpStatus;
                            retAts.payInfoCode = retAts.transState;
                        }
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpSbMsg.toString();
                }
            } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                retAts.respCode = "0";
                retAts.respInfo = tmpStrRet[1];
                retAts.reqSeqID = p_reqBat.reqBatchNO;
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = tmpStrRet[0];
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }

        } catch (Exception e) {
            retAts.respCode = "-1";
            retAts.respInfo = e.toString();
            e.printStackTrace();
        }
        if (retAts.respCode.equals("-1") && StringHelper.isNullOrEmpty(retAts.reqSeqID)) {
            retAts.reqSeqID = p_reqBat.reqBatchNO;
        }
        retAts.writeRetToATS();
        return retAts;
    }

    //1998 状态查询
    public RetAtsQryBatPayRec d_1998_QryBatPay(ReqQryBatPayRec p_req1998) throws Exception {
        return d_QryBatPayRec(p_req1998);
    }

    //9198
    @Override
    public RetAtsQryBatPayRec d_9198_QryBatPay(ReqQryBatPayRec p_req9198) throws Exception {
        return null;
    }

    //批量代付和工资代付状态查询
    public RetAtsQryBatPayRec d_QryBatPayRec(ReqQryBatPayRec p_reqQryBat) throws Exception {
        //返回ATS的对象
        RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_reqQryBat.reqHeader);
        retAts.respCode = "";
        ZYB01Header tmpBnkHeader = new ZYB01Header();
        tmpBnkHeader.trsCode = "05B2EBankBatchTransQry";
        XmlTextWriter xmlWriter = null;
        xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        writeCommonHeader(xmlWriter, tmpBnkHeader, reqHeader.reqSeqID);//写入通用的头部节点
        xmlWriter.writeStartElement("Body");
        xmlWriter.writeElementString("FlowNo", retAts.retHeader.reqSeqID);
        xmlWriter.writeEndElement();
        xmlWriter.writeEndDocument();
        try {
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), tmpBnkHeader, "0", "GBK");
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpStatus = tmpBkReader.getFirstText("/Reply_Msg/Sys_Head/ReturnCode");
                    String tmpStatusText = tmpBkReader.getFirstText("/Reply_Msg/Sys_Head/ReturnMsg");
                    if (tmpStatus != null && tmpStatus.length() > 0) {
                        retAts.respCode = "0";
                        retAts.reqSeqID = p_reqQryBat.reqBatchNO;
                        int tmpCountRow = tmpBkReader.countElement("/Reply_Msg/Body/TotalCount");
                        if (tmpCountRow > 0 && isActiveRetCode(tmpStatus)) {
                            retAts.writeBeforeDetail();
                            while (tmpBkReader.read("/Reply_Msg/Body/List/Row")) {
								/*<Body>
								<TotalCount>2</TotalCount>
								<TotalAmount>2.00</TotalAmount>
								<List>
								<Row>
								<BatchNo>1850062</BatchNo>
								<SeqNo>20001</SeqNo>
								<TransDate>2016-05-20</TransDate>
								<PayerAccountNo>527010100909016</PayerAccountNo>
								<PayeeAccountNo>6231383960001088456</PayeeAccountNo>
								<Amount>1.00</Amount>
								<State>OK</State>
								<Remark>1</Remark>
								</Row>
								</List>
								</Body>
								</Reply_Msg>*/
                                RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();
                                tmpRetDetail.payAct = tmpBkReader.readedXElement.getSingleText("PayerAccountNo");
                                tmpRetDetail.recAct = tmpBkReader.readedXElement.getSingleText("PayeeAccountNo");
                                tmpRetDetail.payAmount = tmpBkReader.readedXElement.getSingleText("Amount");
                                tmpRetDetail.result = tmpBkReader.readedXElement.getSingleText("State");
                                tmpRetDetail.postScript = tmpBkReader.readedXElement.getSingleText("Remark");//加入一个编号来确认这条记录信息
                                tmpRetDetail.payInfoCode = tmpRetDetail.result;
                                retAts.writeRetDetail(tmpRetDetail);
                            }
                        } else if (tmpCountRow == 0) {
                            retAts.respCode = "-1";
                            retAts.respInfo = "状态查询结果为空";
                        } else {
                            retAts.respCode = "-1";
                            retAts.respInfo = tmpStatusText;
                        }
                    } else {
                        retAts.respCode = "-1";
                        retAts.respInfo = tmpStatusText;
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpSbMsg.toString();
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            retAts.respCode = "-1";
            retAts.respInfo = e.getMessage();
            e.printStackTrace();
        }
        if (retAts.respCode.equals("-1") && StringHelper.isNullOrEmpty(retAts.reqSeqID)) {
            retAts.reqSeqID = p_reqQryBat.reqBatchNO;
        }
        if (retAts.respCode.equals("-1")) {
            retAts.writeBeforeDetail();
        }
        retAts.writeAfterDetail();
        return retAts;
    }

}
